﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ImageProcessing.Utility;
using System.Configuration;

namespace ImageReview
{
    public partial class Search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            string username = User.Identity.Name;
            username = username.Substring(username.LastIndexOf('\\') + 1);
            if (!IsPostBack)
            {
                Label lblUsername = (Label)this.FindControl("lblUsername");
                lblUsername.Text = "User: ";
                Label lblUsername2 = (Label)this.FindControl("lblUsername2");
                lblUsername2.Text = username;
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string searchTxt = txtSerach.Text.Trim();
            int goodid;
            if (!int.TryParse(searchTxt, out goodid))
            {
                Response.Write("<script type=\"text/javascript\">alert('Search Good ID is incorrect. It must be a integer.');</script>");
                return;
            }
            else
            {
                Database db = Database.Open(ConfigurationManager.AppSettings["dbname"].ToString());
                List<PhotoInfo> infos = db.SearchImagesByGoodID(searchTxt);
                //TODO: display the images 
                string innerHTML = string.Empty;
                foreach (PhotoInfo item in infos)
                {
                    string filename = string.Format("{0}{1}_284.jpg", item.GoodID, item.Suffix);
                    string filevalue = string.Format("{0}{1}", item.GoodID, item.Suffix);
                    string thumbFilename = string.Format("{0}{1}_thumb.jpg", item.GoodID, item.Suffix);
                    string relPath = item.MD5Folder;
                    string relProcessedBaseFolder = ConfigurationManager.AppSettings["processedImageRelPath"].ToString();
                    string processedImage = string.Format("{0}/{1}/{2}", relProcessedBaseFolder, relPath, filename);
                    string processedThumbImage = string.Format("{0}/{1}/{2}", relProcessedBaseFolder, relPath, thumbFilename);
                    string fullPath = ResolveClientUrl(processedImage);
                    string fullThumbPath = ResolveClientUrl(processedThumbImage);
                    string output = @"<div class='item-details'>
                        <div class='image-details'>";
                    output += String.Format(@"<a href='{0}' class='lightbox_images'><img src='{1}' /></a>
                                    </div>
                                    <span class='image-title' >{2}</span>
                                </div>", fullPath, fullThumbPath, filevalue);
                    innerHTML += output;
                }
                this.searchResult.InnerHtml = innerHTML;
            }
        }
    }
}